package www.com.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.style.DynamicDrawableSpan;
import android.view.Gravity;

/**
 * 文字后面拼接图片的方法处理类
 * @author Administrator
 *
 */
public class UISpannableImage extends DynamicDrawableSpan {
	private Drawable mDrawable; //图片对象
	private int mResourceId; //图片资源id
	private Context mContext;
	private boolean mUseGravity; //是否使用Gravity属性
	private int mGravity; //Gravity属性值
	private int mTextAlignment; //图片显示位置属性设置，ALIGN_BASELINE or ALIGN_BOTTOM

	/**
	 * 设置图片资源和位置
	 * @param ctx
	 * @param resourceId 图片id
	 * @param gravity Gravity属性值
	 */
	public void resetWithGravity(Context ctx, int resourceId, int gravity) {
		mContext = ctx;
		mResourceId = resourceId;
		mDrawable = null;
		mUseGravity = true;
		mGravity = gravity;
		mTextAlignment = ALIGN_BASELINE;
	}

	/**
	 * 设置图片资源和位置
	 * @param ctx
	 * @param resourceId 图片id
	 * @param alignment 图片显示位置属性
	 */
	public void resetWithTextAlignment(Context ctx, int resourceId,int alignment) {
		mContext = ctx;
		mResourceId = resourceId;
		mDrawable = null;
		mUseGravity = false;
		mGravity = Gravity.CENTER_VERTICAL;
		mTextAlignment = alignment;
	}

	/**
	 * 构造函数，设置图片资源和位置
	 * @param context
	 * @param resourceId 图片id
	 * @param gravity Gravity属性值
	 */
	public UISpannableImage(Context context, int resourceId, int gravity) {
		super(ALIGN_BASELINE);
		resetWithGravity(context, resourceId, gravity);
	}

	/**
	 * 构造函数，设置图片资源和位置
	 * @param context
	 * @param resourceId 图片id
	 * @param AlignBasline 图片显示位置属性
	 */
	public UISpannableImage(Context context, int resourceId,boolean AlignBasline) {
		super(AlignBasline ? ALIGN_BASELINE : ALIGN_BOTTOM);
		resetWithTextAlignment(context, resourceId,
				AlignBasline ? ALIGN_BASELINE : ALIGN_BOTTOM);
	}

	@Override
	public void draw(Canvas canvas, CharSequence text, int start, int end,
			float x, int top, int y, int bottom, Paint paint) {
		Drawable b = getDrawable();
		canvas.save();

		int transY = bottom - b.getBounds().bottom;
		if (mUseGravity && Gravity.isVertical(mGravity)) {
			switch (mGravity) {
			case Gravity.TOP: {
				transY = 0;
				break;
			}
			case Gravity.CENTER_VERTICAL: {
				final int height = bottom - top;
				final int dheight = b.getBounds().height();
				transY = (height - dheight) / 2;
				break;
			}
			case Gravity.BOTTOM:
			default:
				break;
			}
		} else {
			if (mTextAlignment == ALIGN_BASELINE) {
				transY -= paint.getFontMetricsInt().descent;
			}
		}
		canvas.translate(x, transY);
		b.draw(canvas);
		canvas.restore();
	}

	@Override
	public Drawable getDrawable() {
		Drawable drawable = null;

		if (mDrawable != null) {
			drawable = mDrawable;
		} else {
			try {
				drawable = mContext.getResources().getDrawable(mResourceId);
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return drawable;
	}

}
